/**
 * 添加办公中心地址
 */

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
} );

function button_bind()
{
	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	$( "#target" ).change( target_changed );
}

function target_changed( e )
{
	var user = $( "#target" ).val();

	if( empty( user ) )
		return;

	// 清空所有已选定的中心地址
	$( "#region" ).find( "input:checkbox" ).prop( "checked", false );

	// 加载已选定的咨询师的所选中心地址
	submitter.post( contextPath+"/index/system_resource_search.json", { username: user }, function( d )
	{
		var region_temp = d.region_temp;

		if( !empty( region_temp ) )
		{
			var regions = region_temp.split( "," );

			for( var i = 0; i < regions.length; i++ )
			{
				$( "#" + regions[ i ] ).prop( "checked", true );
			}
		}
	}, "json" );
}

function submit_form()
{
	var data = {};

	// 选定咨询师
	data.target = $( "#target" ).val();

	if( empty( data.target ) )
	{
		alert( "请先选定咨询师目标。" );
		return;
	}

	// 选定中心地址
	var regions = [];

	$( "#region" ).find( "input:checkbox" ).each( function( id )
	{
		if( $( this ).prop( "checked" ) )
			regions.push( $( this ).attr( "id" ) );
	} );
	data.regions = regions.join( "," );

	submitter.post( "system_resource_go.json", data, function( d )
	{
		alert("添加成功");
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
